package org.alwayssuper.protocol.commons.transform.realtimev2025;

import org.alwayssuper.protostar.annotation.Field;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;

/**
 * 2025版整车数据格式 (0x01)
 * 参考GB/T 32960.3-2025标准表10
 * @author alwaysSuper
 */
@ToString
@Data
@Accessors(chain = true)
public class Vehicle {
    @Field(length = 1, desc = "车辆状态:0x01:启动;0x02:熄火;0x03:其他;0xFE:异常;0xFF:无效")
    private int vehicleStatus;
    
    @Field(length = 1, desc = "充电状态:0x01:停车充电;0x02:行驶充电;0x03:未充电;0x04:充电完成;0xFE:异常;0xFF:无效")
    private int chargingStatus;
    
    @Field(length = 1, desc = "运行模式:0x01:纯电;0x02:混动;0x03:燃油;0xFE:异常;0xFF:无效")
    private int operationMode;
    
    @Field(length = 2, desc = "车速:0~5000(表示0km/h~500km/h),精度0.1km/h,0xFFFF无效 (2025版扩大范围)")
    private int speed;
    
    @Field(length = 4, desc = "累计里程:0~999999.9km,精度0.1km,0xFFFFFFFF无效")
    private int odometer;
    
    @Field(length = 2, desc = "总电压:0~60000(表示0V~6000V),精度0.1V,0xFFFF无效 (2025版扩大范围)")
    private int totalVoltage;
    
    @Field(length = 2, desc = "总电流:0~60000(偏移量3000A,表示-3000A~+3000A),精度0.1A,0xFFFF无效 (2025版扩大范围)")
    private int totalCurrent;
    
    @Field(length = 1, desc = "SOC:0~100%,精度1%,0xFF无效")
    private int soc;
    
    @Field(length = 1, desc = "DC-DC状态:0x01:工作;0x02:断开;0xFE:异常;0xFF:无效")
    private int dcDcStatus;
    
    @Field(length = 1, desc = "挡位:0x00:空挡;0x01~0x0D:1~13挡位;0x0E:倒挡;0x0F:停车挡;0xFE:异常;0xFF:无效")
    private int gearPosition;
    
    @Field(length = 2, desc = "绝缘电阻:0~60000kΩ,精度1kΩ,0xFFFF无效")
    private int insulationResistance;
    
    // ==================== 传输值与真实值转换方法 ====================
    
    /**
     * 获取车速值 (km/h)
     * @return 车速，精度0.1km/h，异常返回-1，无效返回-2
     */
    public double getSpeed() {
        if (speed == 0xFFFE) return -1.0; // 异常
        if (speed == 0xFFFF) return -2.0; // 无效
        return speed * 0.1; // 传输值×0.1 = 实际值(km/h)
    }
    
    /**
     * 设置车速值 (km/h)
     * @param speed 车速值，范围0~500km/h
     */
    public void setSpeed(double speed) {
        if (speed < 0 || speed > 500.0) {
            this.speed = 0xFFFF; // 无效值
        } else {
            this.speed = (int) Math.round(speed * 10); // 真实值×10 = 传输值
        }
    }
    
    /**
     * 获取累计里程 (km)
     * @return 累计里程，精度0.1km，异常返回-1，无效返回-2
     */
    public double getOdometer() {
        if (odometer == 0xFFFFFFFE) return -1.0; // 异常
        if (odometer == 0xFFFFFFFF) return -2.0; // 无效
        return odometer * 0.1; // 传输值×0.1 = 实际值(km)
    }
    
    /**
     * 设置累计里程 (km)
     * @param odometer 累计里程值，范围0~999999.9km
     */
    public void setOdometer(double odometer) {
        if (odometer < 0 || odometer > 999999.9) {
            this.odometer = 0xFFFFFFFF; // 无效值
        } else {
            this.odometer = (int) Math.round(odometer * 10); // 真实值×10 = 传输值
        }
    }
    
    /**
     * 获取总电压 (V)
     * @return 总电压，精度0.1V，异常返回-1，无效返回-2
     */
    public double getTotalVoltage() {
        if (totalVoltage == 0xFFFE) return -1.0; // 异常
        if (totalVoltage == 0xFFFF) return -2.0; // 无效
        return totalVoltage * 0.1; // 传输值×0.1 = 实际值(V)
    }
    
    /**
     * 设置总电压 (V)
     * @param totalVoltage 总电压值，范围0~6000V
     */
    public void setTotalVoltage(double totalVoltage) {
        if (totalVoltage < 0 || totalVoltage > 6000.0) {
            this.totalVoltage = 0xFFFF; // 无效值
        } else {
            this.totalVoltage = (int) Math.round(totalVoltage * 10); // 真实值×10 = 传输值
        }
    }
    
    /**
     * 获取总电流 (A)
     * @return 总电流，精度0.1A，异常返回-9999，无效返回-10000
     */
    public double getTotalCurrent() {
        if (totalCurrent == 0xFFFE) return -9999.0; // 异常
        if (totalCurrent == 0xFFFF) return -10000.0; // 无效
        return (totalCurrent - 30000) * 0.1; // (传输值-30000)×0.1 = 真实值(A)，支持负值
    }
    
    /**
     * 设置总电流 (A)
     * @param totalCurrent 总电流值，范围-3000A~+3000A
     */
    public void setTotalCurrent(double totalCurrent) {
        if (totalCurrent < -3000.0 || totalCurrent > 3000.0) {
            this.totalCurrent = 0xFFFF; // 无效值
        } else {
            this.totalCurrent = (int) Math.round(totalCurrent * 10 + 30000); // (真实值×10)+30000 = 传输值
        }
    }
    
    /**
     * 获取SOC百分比 (%)
     * @return SOC百分比，精度1%，异常返回-1，无效返回-2
     */
    public int getSoc() {
        if (soc == 0xFE) return -1; // 异常
        if (soc == 0xFF) return -2; // 无效
        return soc; // 传输值 = 真实值(%)
    }
    
    /**
     * 设置SOC百分比 (%)
     * @param soc SOC百分比，范围0~100%
     */
    public void setSoc(int soc) {
        if (soc < 0 || soc > 100) {
            this.soc = 0xFF; // 无效值
        } else {
            this.soc = soc; // 真实值 = 传输值
        }
    }
    
    /**
     * 获取绝缘电阻 (kΩ)
     * @return 绝缘电阻，精度1kΩ，异常返回-1，无效返回-2
     */
    public int getInsulationResistance() {
        if (insulationResistance == 0xFFFE) return -1; // 异常
        if (insulationResistance == 0xFFFF) return -2; // 无效
        return insulationResistance; // 传输值 = 真实值(kΩ)
    }
    
    /**
     * 设置绝缘电阻 (kΩ)
     * @param insulationResistance 绝缘电阻值，范围0~60000kΩ
     */
    public void setInsulationResistance(int insulationResistance) {
        if (insulationResistance < 0 || insulationResistance > 60000) {
            this.insulationResistance = 0xFFFF; // 无效值
        } else {
            this.insulationResistance = insulationResistance; // 真实值 = 传输值
        }
    }
    
    /**
     * 检查车速是否有效
     * @return true: 有效, false: 异常或无效
     */
    public boolean isSpeedValid() {
        return speed != 0xFFFE && speed != 0xFFFF;
    }
    
    /**
     * 检查电压是否有效
     * @return true: 有效, false: 异常或无效
     */
    public boolean isTotalVoltageValid() {
        return totalVoltage != 0xFFFE && totalVoltage != 0xFFFF;
    }
    
    /**
     * 检查电流是否有效
     * @return true: 有效, false: 异常或无效
     */
    public boolean isTotalCurrentValid() {
        return totalCurrent != 0xFFFE && totalCurrent != 0xFFFF;
    }
    
    /**
     * 检查SOC是否有效
     * @return true: 有效, false: 异常或无效
     */
    public boolean isSocValid() {
        return soc != 0xFE && soc != 0xFF;
    }
}
